package com.example.demo.day.config.learn.design.observable.x1;

import com.example.demo.day.config.learn.design.observable.x1.Board;
import com.example.demo.day.config.learn.design.observable.x1.Observer;

public class StatisticBoard implements Board, Observer {
    private float mMaxTemperature = -Float.MAX_VALUE;
    private float mMinTemperature = -Float.MAX_VALUE;
    private float mSumOfTemperature = 0f;
    private int mCount = 0;

    @Override
    public void display() {
        String text = "最高、最低、平均(温度)";
        text += (mMaxTemperature + "/" + mMinTemperature + "/" + mSumOfTemperature / mCount);
        System.out.println(text);
    }

    @Override
    public void onNotify(float temperature, float humidity, float pressure) {
        if (temperature > mMaxTemperature) {
            this.mMaxTemperature = temperature;
        }
        if (temperature < mMinTemperature) {
            this.mMinTemperature = temperature;
        }
        mSumOfTemperature += temperature;
        mCount++;
        display();
    }

    @Override
    public String toString() {
        return "StatisticBoard{" +
                "mMaxTemperature=" + mMaxTemperature +
                ", mMinTemperature=" + mMinTemperature +
                ", mSumOfTemperature=" + mSumOfTemperature +
                ", mCount=" + mCount +
                '}';
    }
}
